define(function(require, exports, module) {
    "use strict"; 
     var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
    var echarts = require("dist/echarts/init.js");

    var $number_statistics=$("#number_statistics");
    $number_statistics.length && (function(e) {
        var path = $number_statistics.data("path");
        path && method.get(path, function(d) {
            if(config.issucceed(d)){
                echarts.line("number_statistics", d.data)
            }else{
                config.msg.error(d.message);
            }
            
        })
    })(); 
    
    var $amount_statistics=$("#amount_statistics");
    $amount_statistics.length && (function(e) {
        var path = $amount_statistics.data("path");
        path && method.get(path, function(d) {
            if(config.issucceed(d)){
                echarts.line("amount_statistics", d.data)
            }else{
                config.msg.error(d.message);
            }
            
        })
    })();

    require.async(['datatables'], function() {
        $("#statistics").dataTable({
            "processing": true,
            "serverSide": true,
            "aLengthMenu": [20, 30, 50],
            "searching":true,
            "bSort": true,
            "aoColumnDefs": [
                {
                    "bSortable": false,
                    "aTargets": [0],
                    "mRender": function (data, type, full) {
                        var str='<div class="media"><span class="pull-left thumb-sm">  <img alt="John said" src="{1}"></span>' 
                        +'<div class="media-body"><div><a href="{3}">{0}</a></div>'
                          +'<small class="text-muted"><span>'
                         +' [{2}]</span></small> </div> </div>';
                        return str.format(full[0],full[6],full[7],full[8])
                    }
                },
                {
                    "bSortable": false,
                    "aTargets": [6],
                    "mRender": function (data, type, full) {
                        return '<a href="{0}" class="m-xs" title="查看"><i class="fa fa-eye text-muted"></i></a>'.format(full[8])
                    }
                }
            ],
            // "aoColumns" :[
            //     { "bSortable": false, "bSearchable": false },
            //     { "bSortable": false, "bSearchable": false },
            //     { "bSortable": false, "bSearchable": false },
            //      null, null, null,
            //     { "bSortable": false, "bSearchable": false }
            // ],
            "sPaginationType": "full_numbers",
            "ajax": {
                "url": "/Plus/statistics.php",
                "type":"post",
                "data": function ( d ) {
                    d.myKey = "myValue";
                    // d.custom = $('#myInput').val();
                    // etc
                }
            },
             "oLanguage": {
                "sLengthMenu": " <span>每页显示</span>_MENU_ <span>条记录</span> ",
                "sZeroRecords": "对不起，查询不到任何相关数据",
                "sInfo": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                "sInfoEmtpy": "找不到相关数据",
                "sInfoFiltered": "数据表中共为 _MAX_ 条记录)",
                "sProcessing": "正在加载中...",
                "sSearch": "<span>搜索:</span> ",
                "sUrl": "",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": " 上一页 ",
                    "sNext": " 下一页 ",
                    "sLast": " 末页 "
                }
            }
        });
        
    })
        
    $(".js_hide").length && $(document).on("change",".js_hide",function(){
        var $js_vke_close=$(".js_vke_close"),$js_vke_open=$(".js_vke_open"),
        status=$(".js_open_close_status").val(),flag=$(this).data("val"),
        $js_submit_open=$(".js_submit_open"),$js_submit_close=$(".js_submit_close"),
        $js_submit_btn=$(".js_submit_btn"),$js_submit_default=$(".js_submit_default");
        if(flag== 0){
            $("div.an-box").removeClass("hide");
            $("div.an-box").find(".form-group").each(function(){
                $(this).find("input[type='radio']:first").prop("checked",true);
            }); 
            if(flag!=status){
                $js_vke_close.addClass("hide");
                $js_vke_open.removeClass("hide");
                $js_submit_btn.addClass("hide");
                $js_submit_open.removeClass("hide");
            }else{
                $js_vke_close.addClass("hide");
                $js_vke_open.addClass("hide");
                $js_submit_btn.addClass("hide");
                $js_submit_default.removeClass("hide");
            }
            radioSwitch();
        }else{
            $("div.an-box").addClass("hide");
            if(flag!=status){
                $js_vke_close.removeClass("hide");
                $js_vke_open.addClass("hide");
                $js_submit_btn.addClass("hide");
                $js_submit_close.removeClass("hide");
            }else{
                $js_vke_close.addClass("hide");
                $js_vke_open.addClass("hide");
                $js_submit_btn.addClass("hide");
                $js_submit_default.removeClass("hide");
            }
            
        }
    });

    $(".change-type").length && $(document).on("change","input.change-type",function(){
        if($(this).is(":checked")){
            $(this).closest("div.form-group").find("input[type='text']").removeAttr("disabled");
        }else{
            $(this).closest("div.form-group").find("input[type='text']").attr("disabled","disabled").removeClass("error");
            $(this).closest("div.form-group").find("input[type='text']").next("span").remove();
        }
    })
    $(document).on("click",".js_add_nickname",function(){
        var $this=$(this),place=$this.data("place"),
        $input=$(".js_push_text",$this.closest(".form-group"));
        $input.insertAtCaret(place);
        $input.focus();
    });
    var $js_input_switch=$(".js_input_switch");
    $js_input_switch.length&&radioSwitch();

    function radioSwitch(){
        $js_input_switch.each(function(){
            var $this=$(this),$input=$this.next(".js_input_content"),
            $checked=$("input[type='radio']:checked",$this),type=$checked.data("type");
            if(type=="open"){
                $input.removeClass('hide');
                $input.find("input[type='text']").prop("disabled",false);
            }else{
                $input.addClass('hide');
                $input.find("input[type='text']").prop("disabled",true);
            }
        });
    }
    $(document).on("click",".js_input_switch input[type='radio']",function(){
        var $this=$(this),$input=$this.closest(".js_input_switch").next(".js_input_content"),
        type=$this.data("type");;
        if(type=="open"){
            $input.removeClass('hide');
            $input.find("input[type='text']").prop("disabled",false);
        }else{
            $input.addClass('hide');
            $input.find("input[type='text']").prop("disabled",true);
        }
    });
    $(document).on("click",".js_default_img",function(){
        var $this = $(this),
            $hide_input = $this.prevAll("input[type=text]"),
            url=$this.data("img"),
            timg = $this.prevAll("img"),
            $target = $($this.data("target"));
            if ($target.length > 0) {
                $target.attr('src', url).removeClass("hide");
            } else {
                if (timg.length > 0) {
                    timg.attr('src', url).removeClass("hide");
                } else {
                    var tmp = '<img class="thumb_img" src="{0}" style="max-height: 100px;">';
                    $hide_input.before(tmp.format(url))
                }
            }
            $hide_input.val(url);
    });
    $(document).on("click",".js_submit_close,.js_submit_open",function(){
        var $this=$(this),$form=$this.closest('form'),msg=$this.data("msg");
        if($form.valid()){
            config.msg.msgbox.confirm(msg, function(result) {
                if (result) {
                    $form.submit();
                }
            });
        }
        
    });
    $(document).on("keyup","input[data-toggle='calculatehtml']",function(){
        var $this=$(this),$taget=$($this.data("target")),price=$this.data("price"),
        type=$this.data("type"),result=$taget.val() || 0,val=$this.val() || 0,
        rang=$this.data("rang");
        if(type=="price"){
            if(rang!=undefined){
                result=(val*100/price[rang]).toFixed(2);
            }else{
                result=(val*100/price).toFixed(2);
            }
        }else{
            if(rang!=undefined){
                result=(price[0]*val/100).toFixed(2)+"~"+(price[1]*val/100).toFixed(2);
            }else{
                result=(price*val/100).toFixed(2);
            }
            
        }
        $taget.html(result);
    });
  });